package com.ziiwee.reduce.describe;


import com.ziiwee.reduce.describe.models.FieldRuleModel;

import javax.persistence.Column;
import javax.validation.constraints.*;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum FieldRuleType {

    ASSERT_FALSE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(AssertFalse.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须为 false ")};
            }
            return null;
        }
    },
    ASSERT_TRUE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(AssertTrue.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须为 true ")};
            }
            return null;
        }
    },
    DECIMAL_MAX {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(DecimalMax.class).length > 0) {
                DecimalMax a = element.getAnnotationsByType(DecimalMax.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("value", a.value());
                values.put("inclusive", a.inclusive());
                String desc = String.format("%s的最大值为 %s", label, a.value());
                desc += a.inclusive() ? "" : "，但不包含 " + a.value();
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },
    DECIMAL_MIN {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(DecimalMin.class).length > 0) {
                DecimalMin a = element.getAnnotationsByType(DecimalMin.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("value", a.value());
                values.put("inclusive", a.inclusive());
                String desc = String.format("%s的最小值为 %s", label, a.value());
                desc += a.inclusive() ? "" : "，但不包含 " + a.value();
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },
    DIGITS {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Digits.class).length > 0) {
                Digits digits = element.getAnnotationsByType(Digits.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("integer", digits.integer());
                values.put("fraction", digits.fraction());
                String desc = String.format("%s的最大整数位为 %s ,最大小数位为 %s", label, digits.integer(), digits.fraction());
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },
    EMAIL {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Email.class).length > 0) {
                String regexp = element.getAnnotationsByType(Email.class)[0].regexp();
                Map<String, Object> values = new HashMap<>();
                values.put("regexp", regexp);
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, label + "必须为 Email 格式")};
            }
            return null;
        }
    },
    FUTURE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Future.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是将来的时间")};
            }
            return null;
        }
    },
    FUTURE_OR_PRESENT {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(FutureOrPresent.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是当前或将来的时间")};
            }
            return null;
        }
    },
    MAX {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Max.class).length > 0) {
                Max a = element.getAnnotationsByType(Max.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("value", a.value());
                String desc = String.format("%s 必须是一个数字，其值必须小于或等于 %s", label, a.value());
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },
    MIN {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Min.class).length > 0) {
                Min a = element.getAnnotationsByType(Min.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("value", a.value());
                String desc = String.format("%s 必须是一个数字，其值必须大于或等于 %s", label, a.value());
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },
    NEGATIVE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Negative.class).length > 0) {
                Negative a = element.getAnnotationsByType(Negative.class)[0];
                String desc = String.format("%s 必须为严格的负数（即 0 视为无效值）", label);
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, desc)};
            }
            return null;
        }
    },
    NEGATIVE_OR_ZERO {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(NegativeOrZero.class).length > 0) {
                NegativeOrZero a = element.getAnnotationsByType(NegativeOrZero.class)[0];
                String desc = String.format("%s 必须为必须为负数或 0 ", label);
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, desc)};
            }
            return null;
        }
    },
    NOT_BLANK {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(NotBlank.class).length > 0) {
                NotBlank a = element.getAnnotationsByType(NotBlank.class)[0];
                String desc = String.format("%s 不能为 null ,并且必须至少包含一个非空白字符", label);
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, desc)};
            }
            return null;
        }
    },
    NOT_EMPTY {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(NotEmpty.class).length > 0) {
                NotEmpty a = element.getAnnotationsByType(NotEmpty.class)[0];
                String desc = String.format("%s 不能为 null ,也不能为 empty ", label);
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, desc)};
            }
            return null;
        }
    },
    NOT_NULL {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(NotNull.class).length > 0) {
                NotNull a = element.getAnnotationsByType(NotNull.class)[0];
                String desc = String.format("%s 不能为 null ", label);
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, desc)};
            }
            return null;
        }
    },
    PAST {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Past.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是过去的时间")};
            }
            return null;
        }
    },
    PAST_OR_PRESENT {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(PastOrPresent.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是当前或过去的时间")};
            }
            return null;
        }
    },
    PATTERN {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Pattern.class).length > 0) {
                List<FieldRule> list = new ArrayList<>();
                for (Pattern pattern : element.getAnnotationsByType(Pattern.class)) {
                    Map<String, Object> values = new HashMap<>();
                    values.put("regexp", pattern.regexp());
                    list.add(new FieldRuleModel(name(), values, pattern.message()));
                }
                return list.toArray(new FieldRule[]{});
            }
            return null;
        }
    },
    POSITIVE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Positive.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是严格的正数（即0被视为无效值）")};
            }
            return null;
        }
    },
    POSITIVE_OR_ZERO {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(PositiveOrZero.class).length > 0) {
                return new FieldRuleModel[]{new FieldRuleModel(name(), null, label + "必须是正数或 0 ")};
            }
            return null;
        }
    },
    SIZE {
        @Override
        public FieldRule[] support(AnnotatedElement element, String label) {
            if (element.getAnnotationsByType(Size.class).length > 0) {
                Size size = element.getAnnotationsByType(Size.class)[0];
                Map<String, Object> values = new HashMap<>();
                values.put("min", size.min());
                values.put("max", size.max());
                String desc = String.format("%s的长度必须在 %s (含)与 %s (含)之间", label, size.min(), size.max());
                return new FieldRuleModel[]{new FieldRuleModel(name(), values, desc)};
            }
            return null;
        }
    },

//    COLUMN {
//        @Override
//        public FieldRule[] support(AnnotatedElement element, String label) {
//            if (element.getAnnotationsByType(Column.class).length > 0) {
//                Column column = element.getAnnotationsByType(Column.class)[0];
//                if (!column.nullable()) {
//                    return new FieldRuleModel[]{new FieldRuleModel(NOT_NULL.name(), null, String.format("%s 不能为 null ", label))};
//                }
//            }
//            return null;
//        }
//    },
//
//    UNIQUE {
//        @Override
//        public FieldRule[] support(AnnotatedElement element, String label) {
//            if (element.getAnnotationsByType(Column.class).length > 0) {
//                Column column = element.getAnnotationsByType(Column.class)[0];
//                if (column.unique()) {
//                    return new FieldRuleModel[]{new FieldRuleModel(name(), null, String.format("%s 必须是唯一的 ", label))};
//                }
//            }
//            return null;
//        }
//    },

    ;

    public abstract FieldRule[] support(AnnotatedElement element, String label);

}
